import binascii
import datetime
import json
import time
from io import BytesIO

import fitz
import mmh3
from fastapi import APIRouter, Request
from re_common.baselibrary.tools.all_requests.mrequest import MRequest
from re_common.baselibrary.utils.basedir import BaseDir
from re_common.baselibrary.utils.basefile import BaseFile
from re_common.baselibrary.utils.basetime import BaseTime
from re_common.baselibrary.utils.core.requests_core import MsgCode, INSIDE_HEADERS
from re_common.vip.baseencodeid import BaseLngid

from apps.core.m_route import ContextIncludedRoute
from apps.core.return_info import SUCCESS, ReturnInfo, FAILED
from apps.crawler_platform.core_api.base_get_html import GetJournalHtmlDetailExport
from apps.crawler_platform.core_api.models import UpdateTaskModel, DownloadObjS3, DownReportFile
from apps.crawler_platform.core_platform.core_sql import CoreSqlMixin
from apps.crawler_platform.core_platform.g_model import InputPlatformModel, TaskInfoSaveSettingModel, TaskInfoModel, \
    TaskInfoSaveSettingChangeModel, GetArticleHtml, GetCommanderHtml, GetArticleHtmlLinShi
from apps.crawler_platform.util.requestapihelper import RequestApiHelper
from apps.crawler_platform.util.sqlhelper import SQLHelper
from apps.sql_app.mmongodb import Coll
from settings import get_settings

router = APIRouter(route_class=ContextIncludedRoute)


@router.post("/setting/save_task")
async def save_task(request: Request, input: InputPlatformModel[TaskInfoModel]):
    """
    保存任务至mysql及redis缓存
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    data = input.data.dict()
    taskKey = "{}_{}_{}_{}".format(data["task_name"], data["task_tag"], data["groups"], data["order_num"])
    redis = request.app.state.redis
    data["redis_uptime"] = str(datetime.datetime.now())
    res_value = await redis.hset("taskinfo", taskKey, json.dumps(data, ensure_ascii=False))
    # res_value 值为0，redis已存在key覆盖成功，值为1redis不存在key创建保存成功
    if (res_value == 0) or (res_value == 1):
        await CoreSqlMixin.save_taskinfo(data)
        return_info.status = SUCCESS
        return_info.msg_code = MsgCode.SUCCESS_CODE
        return_info.msg = "保存到任务成功"
        return return_info.todict()
    else:
        return_info.status = FAILED
        return_info.msg_code = 400
        return_info.msg = "保存到任务失败"
        return return_info.todict()


@router.post("/setting/update_task")
async def update_task(request: Request, input: InputPlatformModel[UpdateTaskModel]):
    """
    更新任务至mysql及redis缓存
    :param input:#     :return:
    """
    return_info = ReturnInfo()
    data = input.data.dict()

    # sql = f"select * from taskinfo where task_name=%s and task_tag=%s and order_num=%s and `groups`=%s "
    # sql_args = (
    #     data['basekey']['task_name'], data['basekey']['task_tag'],
    #     data['basekey']['order_num'], data['basekey']['groups'])
    # row = await SQLHelper.fetchone(sql, sql_args, aiomysql.DictCursor)
    row = await CoreSqlMixin.select_taskinfo(data)
    del row["create_time"]
    del row["update_time"]
    taskKey = "{}_{}_{}_{}".format(data['basekey']["task_name"],
                                   data['basekey']["task_tag"],
                                   data['basekey']["groups"],
                                   data['basekey']["order_num"])
    redis = request.app.state.redis
    for setinfo in data["upset"]:
        if setinfo['flag'] == 'ture':
            # sql = f"update taskinfo set '{setinfo['key']}' = '{setinfo['value']}' where " \
            #       f"task_name = '{data['basekey']['task_name']}' and task_tag = '{data['basekey']['task_tag']}' and order_num = {data['basekey']['order_num']} and groups = '{data['basekey']['groups']}'"
            row[setinfo['key']] = setinfo['value']
            row["redis_uptime"] = str(datetime.datetime.now())
            res_value = await redis.hset("taskinfo", taskKey, json.dumps(row, ensure_ascii=False))
            if (res_value == 0) or (res_value == 1):
                # await CoreSqlMixin.update_taskinfo(sql)
                await CoreSqlMixin.update_task2(setinfo, data)
                return_info.status = SUCCESS
                return_info.msg_code = MsgCode.SUCCESS_CODE
                return_info.msg = "保存到任务成功"
                return_info.data = ""
                return return_info.todict()
            else:
                return_info.status = FAILED
                return_info.msg_code = 400
                return_info.msg = "保存到任务失败"
                return_info.data = ""
                return return_info.todict()
        else:
            infodict = eval(row[setinfo['key']])
            infodict[setinfo['value']['key']] = setinfo['value']['value']
            row[setinfo['key']] = json.dumps(infodict, ensure_ascii=False)

            # sql = f"update taskinfo set {setinfo['key']} = '{Mysql.escape(str(infodict))}' where " \
            #       f"task_name = '{data['basekey']['task_name']}' and task_tag = '{data['basekey']['task_tag']}' and order_num = {data['basekey']['order_num']} and `groups` = '{data['basekey']['groups']}'"
            row["redis_uptime"] = str(datetime.datetime.now())
            redis.hset("taskinfo", taskKey, json.dumps(row, ensure_ascii=False))
            res_value = await redis.hset("taskinfo", taskKey, json.dumps(row, ensure_ascii=False))
            if (res_value == 0) or (res_value == 1):
                await CoreSqlMixin.update_task(setinfo, infodict, data)
                # await CoreSqlMixin.update_taskinfo(sql)
                return_info.status = SUCCESS
                return_info.msg_code = MsgCode.SUCCESS_CODE
                return_info.msg = "保存到任务成功"
                return_info.data = ""
                return return_info.todict()
            else:
                return_info.status = FAILED
                return_info.msg_code = 400
                return_info.msg = "保存到任务失败"
                return_info.data = ""
                return return_info.todict()


@router.post("/setting/save_taskinfo_setting")
async def save_taskinfo_setting(request: Request, input: InputPlatformModel[TaskInfoSaveSettingModel]):
    """
    保存任务配置表至mysql及redis缓存
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    data = input.data.dict()
    taskKey = "{}_{}".format(data["task_name"], data["task_tag"])
    redis = request.app.state.redis
    data["redis_uptime"] = str(datetime.datetime.now())
    res_value = await redis.hset("taskinfo_save_setting", taskKey, json.dumps(data, ensure_ascii=False))
    if (res_value == 0) or (res_value == 1):
        await CoreSqlMixin.save_taskinfo_set(data)
        return_info.status = SUCCESS
        return_info.msg_code = MsgCode.SUCCESS_CODE
        return_info.msg = "保存到任务配置成功"
        return return_info.todict()
    else:
        return_info.status = FAILED
        return_info.msg_code = 400
        return_info.msg = "保存任务配置失败"
        return return_info.todict()


@router.post("/setting/change_save_taskinfo_setting")
async def save_taskinfo_setting(input: InputPlatformModel[TaskInfoSaveSettingChangeModel]):
    """
    根据输入参数进行控制下载任务
    ZCN
    :param input:\n
            task_name:任务名，一般一个网站一个任务名

            task_tag: 任务标签，一般一个步骤一个任务标签，比如期刊分为cnkijournallist，cnkiissue，cnkiarticle
                      当然也兼容一些特殊的情况，比如海外版,网络首发等

            database_table_name: 用作表示子任务所属表

            table_name: taskinfo_save_setting表中的table_name字段

            switch: 用于确定本次请求归属, 0: 表示本次请求输入的参数 为关闭参数(即将输入tag_state或task_state置为0,其余置为1)
                    1: 表示本次请求输入的参数 为开启参数(即将输入tag_state或task_state置为1, 其余置为0)

            is_task: 用于控制本次请求是否为整个任务控制, 0: 表示本次请求不是以task_state 为整体(即将task_state置为1, 其余不做操作);
                     1: 表示本次请求是控制整个任务是以task_state 为整体(即将task_state置为1, 其余置为0)
    :return:
    """

    return_info = ReturnInfo()
    task_name = input.data.task_name
    task_tag = input.data.task_tag
    switch = input.data.switch
    database_table_name = input.data.database_table_name
    table_name = input.data.table_name
    is_task = input.data.is_task
    if is_task == "0":
        # 这种情况下  无论如何也要保证task_state = 1 以免下面 的另一种情况对这边造成干扰
        if switch == "0":
            sql_up = f"update {database_table_name} set task_state = 1, tag_state = 0 where task_name = '{task_name}' and task_tag = '{task_tag}'"
        elif switch == "1":
            sql_up = f"update {database_table_name} set task_state = 1, tag_state = 1 where task_name = '{task_name}' and task_tag = '{task_tag}'"
        else:
            return_info.status = FAILED
            return_info.msg_code = MsgCode.SETTING_CONFIG_ERROR
            return_info.msg = "更新taskinfo_save_setting的任务控制配置失败(switch参数错误,只能为0或1)"
            return_info.data = ""
            return return_info.todict()

    elif is_task == "1":
        if switch == "0":
            sql_up = f"update {database_table_name} set task_state = 0, tag_state = 0 where task_name = '{task_name}'"
        elif switch == "1":
            sql_up = f"update {database_table_name} set task_state = 1, tag_state = 1 where task_name = '{task_name}'"
        else:
            return_info.status = FAILED
            return_info.msg_code = 251
            return_info.msg = "更新taskinfo_save_setting的任务控制配置失败(switch参数错误,只能为0或1)"
            return_info.data = ""
            return return_info.todict()
    else:
        return_info.status = FAILED
        return_info.msg_code = 251
        return_info.msg = "更新taskinfo_save_setting的任务控制配置失败(is_task参数错误,只能为0或1)"
        return_info.data = ""
        return return_info.todict()
    await SQLHelper.execute_commit(sql_up)
    return_info.status = SUCCESS
    return_info.msg_code = MsgCode.SUCCESS_CODE
    return_info.msg = "更新taskinfo_save_setting的任务控制配置成功"
    return_info.data = ""
    return return_info.todict()


@router.get("/service/reload")
async def get_reload():
    '''
    更新包后强制重加载
    ZCN:有问题,写文件并不能重加载,可能得写其他东西, 尚未完成
        如果要在目前情况下进行重加载,那么需要在init文件里面加入一个导入内容, 然后对这个文件内容进行修改, 例如往test.py文件里面写入batch
    :return:
    '''
    return_info = ReturnInfo()
    cur_path = BaseDir.get_file_dir_absolute(__file__)
    top_path = BaseDir.get_upper_dir(cur_path, -3)
    spath = BaseFile.get_new_path(top_path, "reload.db")
    basetime = BaseTime()
    batchtime = basetime.get_beijin_date_strins()
    batch = batchtime[0:8] + "_" + batchtime[-6:]
    dicts = {"batch": batch}
    dicts_json = json.dumps(dicts)
    BaseFile.single_write_file(spath, dicts_json)
    aaa = BaseFile.read_file_rb(spath)
    return_info.status = SUCCESS
    return_info.msg_code = MsgCode.SUCCESS_CODE
    return_info.msg = "更新完成"
    return_info.data = ""
    return return_info.todict()


@router.post("/get_html/get_article_html")
async def get_article_html(request: Request, input: InputPlatformModel[GetArticleHtml]):
    return_info = ReturnInfo()
    redis = request.app.state.redis
    lngid = input.data.lngid
    sub_db_id = lngid[0:5]
    res_value = await redis.hget("sub_db_id", sub_db_id)
    if res_value is None:
        return_info.status = FAILED
        return_info.msg_code = 599
        return_info.msg = "需提取的数据并未进入数据库提取白名单, 请联系工作人员"
        return_info.data = ""
        return return_info.todict()
    else:
        res_value_json = json.loads(res_value)
        task_name = res_value_json["task_name"]
        task_tag = res_value_json["task_tag"]
        table_name = res_value_json["table_name"]
        latest = res_value_json["latest"]
        # if sub_db_id == "00002":
        #     conn_latest = Coll.get_table_conn(latest)
        #     data_latest = await conn_latest.find_one({"_id": lngid})
        #     if data_latest is not None:
        #         journal_raw_id = data_latest["journal_raw_id"]
        #         pub_year = data_latest["pub_year"]
        #         num = data_latest["num"]
        #         title = data_latest["title"]
        #         rawid = ",".join([journal_raw_id, pub_year, num, title])
        #     else:
        #         return_info.status = FAILED
        #         return_info.msg_code = 598
        #         return_info.msg = "{}数据库内无此数据, 请检查{}".format(latest, lngid)
        #         return_info.data = ""
        #         return return_info.todict()
        # else:
        #     rawid = BaseLngid().GetRawid(lngid.lstrip(sub_db_id))
        conn_latest = Coll.get_table_conn(latest)
        data_latest = await conn_latest.find_one({"_id": lngid})
        if data_latest is not None:
            rawid = data_latest["rawid_mysql"]
        else:
            return_info.status = FAILED
            return_info.msg_code = 598
            return_info.msg = "{}数据库内无此数据, 请检查{}".format(latest, lngid)
            return_info.data = ""
            return return_info.todict()
        _id = f"{task_name}_{task_tag}_{rawid}"
        conn = Coll.get_table_conn(table_name)
        data = await conn.find_one({"_id": _id})
        if data is not None:
            last_data = data["last_data"]
            return_info.status = SUCCESS
            return_info.msg_code = MsgCode.SUCCESS_CODE
            return_info.msg = "更新完成"
            return_info.data = last_data
            return return_info.todict()
        else:
            return_info.status = FAILED
            return_info.msg_code = 597
            return_info.msg = "{}数据库内无此数据, 请检查{}".format(table_name, _id)
            return_info.data = ""
            return return_info.todict()


@router.post("/get_html/get_article_html_linshi")
async def get_article_html(request: Request, input: InputPlatformModel[GetArticleHtmlLinShi]):
    return_info = ReturnInfo()
    a_list = list()
    a_dict = dict()
    b_list = list()
    redis = request.app.state.redis
    lngid_list = input.data.lngid_list
    sub_db_id = input.data.sub_db_id
    get_dict = input.data.get_dict
    res_value = await redis.hget("sub_db_id", sub_db_id)
    res_value_json = json.loads(res_value)
    task_name = res_value_json["task_name"]
    task_tag = res_value_json["task_tag"]
    table_name = res_value_json["table_name"]
    latest = res_value_json["latest"]
    conn_latest = Coll.get_table_conn(latest)
    start_time = time.time()
    data_latest = conn_latest.find({"_id": {"$in": lngid_list}}, {"rawid_mysql": 1})
    async for adata in data_latest:
        key = adata["_id"]
        rawid = adata["rawid_mysql"]
        _id = f"{task_name}_{task_tag}_{rawid}"
        a_dict[key] = _id
        # _id = f"{task_name}_{task_tag}_{rawid}"
        a_list.append(_id)
    conn = Coll.get_table_conn(table_name)
    end_time = time.time()
    # data_list = conn.find({"_id": {"$in": a_list}}, {"last_data.down_dict.1_1": 1, "last_data.down_dict.1_3": 1})
    data_list = conn.find({"_id": {"$in": a_list}}, get_dict)
    aaa_time = time.time()
    async for data in data_list:
        local_dict = dict()
        last_data = data["last_data"]
        down_dict = last_data["down_dict"]
        for local_key, local_value in a_dict.items():
            if local_value == data["_id"]:
                local_dict[local_key] = down_dict
                b_list.append(local_dict)
    end_time_2 = time.time()
    return_info.status = SUCCESS
    return_info.msg_code = MsgCode.SUCCESS_CODE
    return_info.msg = "更新完成"
    # return_info.data = a_list
    return_info.data = b_list
    return return_info.todict()


# @router.post("/get_html/commander")
# async def set_html_info(request: Request, input: InputPlatformModel[GetCommanderHtml]):
#     """
#     提取html接口
#     :param request:
#     :param input:
#     :return:
#     """
#     return_info = ReturnInfo()
#     infos = input.data.infos
#     redis = request.app.state.redis
#     sub_db_id = input.data.sub_db_id
#     get_type = input.data.get_type
#     id_list = input.data.id_list
#     res_value = await redis.hget("sub_db_id_test", sub_db_id)
#     res_value_json = json.loads(res_value)
#     if res_value is None:
#         return_info.status = FAILED
#         return_info.msg_code = 599
#         return_info.msg = "需提取的数据并未进入数据库提取白名单, 请联系工作人员"
#         return_info.data = ""
#         return return_info.todict()
#     start_time = time.time()
#     solve_value = GetJournalHtmlDetail()
#     this_task = res_value_json.get(get_type)
#     if this_task is not None:
#         bools, html_list = await solve_value.run(get_type, this_task, infos, id_list)
#         if bools:
#             end_time = time.time()
#             return_info.status = SUCCESS
#             return_info.msg_code = 200
#             return_info.msg = "提取完成"
#             return_info.data = html_list
#             return return_info.todict()
#         else:
#             return_info.status = FAILED
#             return_info.msg_code = 599
#             return_info.msg = html_list
#             return_info.data = ""
#             return return_info.todict()
#     else:
#         return_info.status = FAILED
#         return_info.msg_code = 599
#         return_info.msg = "该任务没有该get_type: {}, 请检查".format(get_type)
#         return_info.data = ""
#         return return_info.todict()


@router.post("/get_html/exporter")
async def set_html_info(request: Request, input: InputPlatformModel[GetCommanderHtml]):
    """
    提取html接口,分为两种类型, get_type为latest时 有效字段: sub_db_id, infos, id_list(例1)
    get_type为not_latest时 有效字段: task_name, task_tag, infos, id_list(例2)
    :param request:
    :param input:
                get_type: str = 'not_latest'
                    有latest和not_latest两种标志
                task_name: str = ''
                task_tag: str = ''
                sub_db_id: str = ''
                    例: 00002
                infos: dict = {}
                    查询条件, 例: {"last_data.down_dict.1_1": 1}
                id_list: List[dict] = []
                    例1: [{"task_name": 'wanfangjournal',"task_tag": 'wanfangqkclasshome',"home_rawid": bjson}]
                    例2: [{"lngid":"00002AAAAAAAAAAAAAAAAAAAAAAAAA"}]
    :return:
    """
    return_info = ReturnInfo()
    infos = input.data.infos
    redis = request.app.state.redis
    sub_db_id = input.data.sub_db_id
    get_type = input.data.get_type
    task_name = input.data.task_name
    task_tag = input.data.task_tag
    id_list = input.data.id_list
    if sub_db_id != "" and get_type == "latest":
        res_value = await redis.hget("html_export_info", sub_db_id)
    else:
        res_value = await redis.hget("html_export_info", "_".join([task_name, task_tag]))

    if res_value is None:
        return_info.status = FAILED
        return_info.msg_code = 599
        return_info.msg = "需提取的数据并未进入数据库提取白名单, 请联系工作人员"
        return_info.data = ""
        return return_info.todict()
    start_time = time.time()
    solve_value = GetJournalHtmlDetailExport()
    res_value_json = json.loads(res_value)
    bools, html_list = await solve_value.run(get_type, res_value_json, infos, id_list)
    if bools:
        end_time = time.time()
        return_info.status = SUCCESS
        return_info.msg_code = MsgCode.SUCCESS_CODE
        return_info.msg = "提取完成"
        return_info.data = html_list
        return return_info.todict()
    else:
        return_info.status = FAILED
        return_info.msg_code = 599
        return_info.msg = html_list
        return_info.data = ""
        return return_info.todict()


def init_file_dic():
    file_dic = {
        "_id": "",
        "filename": "",
        "keyid": "",
        "lngid": "",
        "sub_db_id": "00780",
        "raw_source_url": "",
        "source_type": "12",
        "latest_date": "",
        "is_deprecated": "0",
        "fulltext_type": "",
        "fulltext_addr": "",
        "fulltext_size": "",
        "fulltext_txt": "",
        "page_cnt": "",
        "pub_year": ""
    }
    return file_dic


def addr_hash(filename):
    hash_code = binascii.b2a_hex(mmh3.hash_bytes(filename)).upper()[0:3]
    hash_name = [chr(hash_code[0]), chr(hash_code[1]), chr(hash_code[2])]
    if hash_name[2] == 'D':
        hash_name[2] = 'A'
    elif hash_name[2] == 'E':
        hash_name[2] = 'B'
    elif hash_name[2] == 'F':
        hash_name[2] = 'C'
    elif hash_name[2].isdigit():
        hash_name[2] = str(int(hash_name[2]) % 5)
    return ''.join(hash_name)

def transform(pub_year, filename):
    return 'report_file_new/' + pub_year + '/' + addr_hash(filename) + '/' + filename



@router.post("/uploadobj/uploadfile")
async def upload_obj2s3(input: InputPlatformModel[DownReportFile]):
    return_info = ReturnInfo()
    from settings import get_settings
    from app_main import app
    sets = get_settings()
    url = input.data.url
    # save_path = input.data.file_dir
    task_name = input.data.task_name
    task_tag = input.data.task_tag
    rawid = input.data.rawid
    # file_type = input.data.file_type
    save_path = input.data.save_path
    data_type = input.data.data_type
    data_num = input.data.data_num
    ext_name = input.data.ext_name
    data_dic = input.data.data_dic
    proxy_type = input.data.proxy_type
    google_proxy = input.data.google_proxy
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36'
    }
    rrq = RequestApiHelper.get_rrq()
    kwargs = {
        "rrq": rrq,
        "header": headers,
        "url": url,
        "timeout": 300,
        # "proxy": proxies,
        "moths": MRequest.GET,
        "middler_list": [],
        "allow_resp_text": False,
        "allow_resp_bytes": True,
    }
    boolsg, dicts = await RequestApiHelper.down_obj(proxy_type, google_proxy, **kwargs)

    if boolsg:
        cont_obj = rrq.html_bytes
        # print(rrq.resp.headers)
        # 根据返回的headers里面进行pdf判定
        judge_ext_name = rrq.resp.headers.get("Content-Disposition", "")
        if ext_name == "other":
            if ".pdf" in judge_ext_name:
                ext_name = "pdf"
                keyid = data_dic.get("keyid", "")
                pub_year = data_dic.get("pub_year", "")
                new_file_name = keyid + ".pdf"
                save_path = transform(pub_year, new_file_name)
                data_dic["fulltext_addr"] = save_path
                data_dic["_id"] = new_file_name
                data_dic["filename"] = new_file_name
                data_dic["fulltext_type"] = "pdf"
        fulltext_size = len(cont_obj)
        bytesIO = BytesIO(cont_obj)
        if ext_name == "pdf":
            try:
                doc = fitz.Document(stream=bytesIO, filetype="pdf")
                page_cnt = doc.page_count
            except:
                sql = f'update qy_report_article_new set failcount = failcount + 1 where task_name = "{task_name}" and task_tag = "{task_tag}" and rawid = "{rawid}" and data_type = "{data_type}" and data_num = "{data_num}"'
                await SQLHelper.execute_commit(sql)
                return_info.status = FAILED
                return_info.msg_code = MsgCode.API_FAIL_CODE
                return_info.msg = "PDF打开失败"
                return_info.data = ""
                return return_info.todict()
        # 这代表在上面的时候没有获取到pdf后缀
        elif ext_name == "other":
            fulltext_type = data_dic["fulltext_type"]
            sql = f'update qy_report_article_new set state = 4, real_ext_name = "{fulltext_type}" where task_name = "{task_name}" and task_tag = "{task_tag}" and rawid = "{rawid}" and data_type = "{data_type}" and data_num = "{data_num}"'
            await SQLHelper.execute_commit(sql)
            return_info.status = FAILED
            return_info.msg_code = MsgCode.API_FAIL_CODE
            return_info.msg = "other 类型经下载后依然不能判别类型, 放弃存储"
            return_info.data = ""
            return return_info.todict()
        else:
            page_cnt = 1

        async with app.state.aioboto3.client('s3', endpoint_url=sets.AWS_LOCAL_ENDPOINT_URL) as s3:
            s3_ob = await s3.upload_fileobj(bytesIO, sets.AWS_LOCAL_BUCKET, save_path)
            chk_one = await s3.head_object(Bucket=sets.AWS_LOCAL_BUCKET, Key=save_path)
            if chk_one is None:
                sql = f'update qy_report_article_new set failcount = failcount + 1 where task_name = "{task_name}" and task_tag = "{task_tag}" and rawid = "{rawid}" and data_type = "{data_type}" and data_num = "{data_num}"'
                await SQLHelper.execute_commit(sql)
                return_info.status = FAILED
                return_info.msg_code = MsgCode.API_FAIL_CODE
                return_info.msg = "存储失败"
                return_info.data = ""
                return return_info.todict()
            else:
                fulltext_type = data_dic["fulltext_type"]
                data_dic["page_cnt"] = page_cnt
                data_dic["fulltext_size"] = fulltext_size
                data_dic["is_deprecated"] = "0"
                fulltext_client = Coll().get_cqvipqyreport_fulltext_latest_new()
                acjp = await fulltext_client.replace_one({"_id": data_dic["_id"]}, data_dic, upsert=True)
                # print(acjp.matched_count)
                # print(acjp.modified_count)
                # 两个数字为0 表示插入了一条    两个数字为1 表示替换了一条, 不过不确定, 得再观察
                sql = f'update qy_report_article_new set state = 1, real_ext_name = "{fulltext_type}" where task_name = "{task_name}" and task_tag = "{task_tag}" and rawid = "{rawid}" and data_type = "{data_type}" and data_num = "{data_num}"'
                await SQLHelper.execute_commit(sql)
                return_info.status = SUCCESS
                return_info.msg_code = MsgCode.SUCCESS_CODE
                return_info.msg = "存储到S3成功"
                return_info.data = ""
                return return_info.todict()

    else:
        sql = f'update qy_report_article_new set failcount = failcount + 1 where task_name = "{task_name}" and task_tag = "{task_tag}" and rawid = "{rawid}" and data_type = "{data_type}" and data_num = "{data_num}"'
        await SQLHelper.execute_commit(sql)
        return_info.status = FAILED
        return_info.msg_code = MsgCode.API_FAIL_CODE
        return_info.msg = "下载失败"
        return_info.data = ""
        return return_info.todict()


